package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

abstract class PlayingState implements GameState {
    private final CodeRebirthGame game;
    private JTextArea codeArea;
    private JLabel storyLabel;
    private static final int WIDTH = 1024;
    private static final int HEIGHT = 768;

    public PlayingState(CodeRebirthGame game) {
        this.game = game;
    }

    @Override
    public void enter() {
        game.setLayout(new BorderLayout());

        // 故事面板 - 增加背景和边距
        JPanel storyPanel = new JPanel(new BorderLayout());
        storyPanel.setBackground(new Color(20, 25, 40));
        storyPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));

        storyLabel = new JLabel(getStoryHtml(), JLabel.CENTER);
        storyLabel.setFont(game.getResourceManager().getFont("main", 18f));
        storyLabel.setForeground(new Color(220, 240, 255)); // 亮青色文字
        storyPanel.add(storyLabel);

        // 代码编辑区 - 提高可读性
        codeArea = new JTextArea();
        codeArea.setFont(game.getResourceManager().getFont("code", 16f)); // 增大字号
        codeArea.setBackground(new Color(25, 30, 45)); // 深色背景
        codeArea.setForeground(new Color(150, 250, 150)); // 亮绿色代码
        codeArea.setCaretColor(Color.WHITE);
        codeArea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 挑战描述面板
        JPanel challengePanel = new JPanel(new BorderLayout());
        challengePanel.setBackground(new Color(30, 35, 60));
        challengePanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(70, 130, 180), 2),
                BorderFactory.createEmptyBorder(10, 15, 10, 15)
        ));

        JLabel challengeLabel = new JLabel(
                "<html><div style='width:550px;color:#66CCFF;font-size:14pt;'>" +
                        game.getChallengeManager().getCurrentChallenge().getDescription() +
                        "</div></html>"
        );
        challengePanel.add(challengeLabel);

        // 按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setBackground(new Color(30, 40, 60));
        JButton submitBtn = new JButton("提交代码");
        submitBtn.addActionListener(this::handleSubmit);
        styleButton(submitBtn);

        JButton saveBtn = new JButton("保存进度");
        saveBtn.addActionListener(e -> showMessage("游戏已保存"));
        styleButton(saveBtn);

        buttonPanel.add(saveBtn);
        buttonPanel.add(submitBtn);

        // 布局调整
        JPanel centerPanel = new JPanel(new BorderLayout());
        centerPanel.add(challengePanel, BorderLayout.NORTH);
        centerPanel.add(new JScrollPane(codeArea), BorderLayout.CENTER);

        game.add(storyPanel, BorderLayout.NORTH);
        game.add(centerPanel, BorderLayout.CENTER);
        game.add(buttonPanel, BorderLayout.SOUTH);

    }

    private void handleSubmit(ActionEvent e) {
        String code = codeArea.getText();
        if (code.trim().isEmpty()) {
            showMessage("请输入代码");
            return;
        }

        if (game.getChallengeManager().submitSolution(code)) {
            game.getPlayerData().completeChallenge();
            showMessage("挑战成功!");

            if (shouldAdvanceStory()) {
                game.getStoryManager().advanceStory();
                updateStoryDisplay();
            }

            game.getChallengeManager().generateNewChallenge();
            updateChallengeDisplay();
            codeArea.setText("");
        } else {
            showMessage("挑战失败!\n提示: " + game.getChallengeManager().getCurrentChallenge().getHint());
        }
    }

    private boolean shouldAdvanceStory() {
        return game.getPlayerData().getCompletedChallenges() % 3 == 0;
    }

    private void updateStoryDisplay() {
        storyLabel.setText(getStoryHtml());
    }

    private void updateChallengeDisplay() {
        storyLabel.setText(getStoryHtml() + "<br><br>" +
                "<font color='#66CCFF'>当前挑战:</font><br>" +
                game.getChallengeManager().getCurrentChallenge().getDescription());
    }

    private String getStoryHtml() {
        return "<html><div style='width:600px;color:white;'>" +
                game.getStoryManager().getCurrentStory() + "</div></html>";
    }

    private void styleButton(JButton button) {
        button.setFont(game.getResourceManager().getFont("main", 14f));
        button.setBackground(ResourceManager.UI_BLUE);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
    }

    private void showMessage(String msg) {
        JOptionPane.showMessageDialog(game, msg);
    }

    @Override
    public void render(Graphics g) {
        // 渐变背景
        GradientPaint gradient = new GradientPaint(0, 0, new Color(20, 30, 40),
                0, HEIGHT, new Color(40, 50, 60));
        ((Graphics2D) g).setPaint(gradient);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        // 绘制角色状态
        g.setFont(game.getResourceManager().getFont("main", 14f));
        g.setColor(ResourceManager.TEXT_WHITE);
        g.drawString("系统健康度: " + game.getPlayerData().getSystemHealth() + "%", 20, 30);
    }

    @Override
    public void update() {
    }

    @Override
    public void exit() {
        game.getContentPane().removeAll();
    }

    @Override
    public void handleInput(KeyEvent e) {
    }
}
